package com.ws.shop.action.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ws.shop.bean.Page;
import com.ws.shop.entity.Article;
import com.ws.shop.entity.ArticleCategories;
import com.ws.shop.service.ArticleCategoriesService;
import com.ws.shop.service.ArticleService;
import com.ws.shop.service.HtmlService;
import com.ws.shop.util.RequestUtil;

@Controller
@RequestMapping(value="/admin/buildhtml/*")
public class BuildHtmlAction extends BaseAdminAction{
	@Autowired
	private ArticleService articleService;
	@Autowired
	private ArticleCategoriesService articleCategoriesService;
	@Autowired
	private HtmlService htmlService;
	
	@RequestMapping(value="allinput.jhtml",method=RequestMethod.GET)
	public String allinput(HttpServletRequest request){
		return "build_html_all_input";
	}

	@RequestMapping(value="all.jhtml",method=RequestMethod.POST)
	public String all(){
		
		return null;
	}
	
	@RequestMapping(value="articleinput.jhtml",method=RequestMethod.GET)
	public String articleinput(HttpServletRequest request,Model model){
		List<ArticleCategories> articleCategoriesTreeList = articleCategoriesService.findArticleCategoriesTreeList();
		model.addAttribute("articleCategoriesTreeList", articleCategoriesTreeList);
		return "build_html_article_input";
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="article.jhtml",method=RequestMethod.POST)
	public String article(HttpServletRequest request,HttpServletResponse response){
		try{
		int cid = RequestUtil.getIntParameter(request, "cid", 0);
		String start = RequestUtil.getStringParameter(request, "start", null);
		String end = RequestUtil.getStringParameter(request, "end", null);
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		int pagesize = RequestUtil.getIntParameter(request, Page.PAGE_SIZE, 50);
		int[] cids = null;
		if(cid > 0){
			List<ArticleCategories> list = articleCategoriesService.findAllChildArticleCategoriesById(cid,1);
			cids = new int[list.size()];
			for(int i = 0 ; i < list.size() ; i++){
				cids[i] = list.get(i).getId();
			}
		}
		Page page = articleService.findArticlesByCids(pageno,pagesize,cids,start,end);
		List<Article> articleList = (List<Article>)page.getData();
		if (articleList != null && articleList.size() > 0) {
			for (Article article : articleList) {
				htmlService.articleContentBuildHtml(article);
			}
		}
		Map<String, String> jsonMap = new HashMap<String, String>();
		if (articleList != null && articleList.size() == pagesize) {
			jsonMap.put(STATUS, "articleBuilding");
		} else {
			jsonMap.put(STATUS, "articleFinish");
		}
		jsonMap.put("updateRows", articleList.size()+"");
		return this.ajaxJson(response, jsonMap);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
}
